﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Assignment2
{
    /// <summary>
    /// Encodes an order into a String for transport via the multi-cell buffer
    /// </summary>
    class Encoder
    {
        private Order order;

        /// <summary>
        /// Constructor to initialize the encoder.
        /// </summary>
        /// <param name="order">Order object.</param>
        public Encoder(Order order)
        {
            this.order = order;
        }

        /// <summary>
        /// Encodes the order by serializing the object as a String
        /// </summary>
        /// <returns>Encoded order as a String</returns>
        public string encode()
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, order);

            return Convert.ToBase64String(memoryStream.ToArray());
        }
    }
}
